package com.bone.modules.system.service.impl;

import com.bone.common.util.BoneUtils;
import com.bone.modules.system.dto.UserLoginDto;
import com.bone.modules.system.entity.SysUser;
import com.bone.modules.system.mapper.SysUserMapper;
import com.bone.modules.system.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 王可可
 * @since 2020-12-07
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public Boolean accountRegister(UserLoginDto userLoginDto) {
        // 获取MD5加密的盐值
        String randomSalt = BoneUtils.getRandomSalt();
        // 密码加密
        String md5Password = BoneUtils.encrypt(userLoginDto.getUsername(), userLoginDto.getPassword(), randomSalt);
        // 数据持久化
        SysUser umsUser = new SysUser()
                .setUsername(userLoginDto.getUsername())
                .setPassword(md5Password)
                .setSalt(randomSalt);
        baseMapper.insert(umsUser);
        // 通过MP的插入回显 - 来判断用户是否注册成功
        log.info("用户注册接口实现插入 >>>>>>用户ID：" + umsUser.getId());
        return BoneUtils.isNotEmpty(umsUser.getId());
    }
}
